package com.clkj.cldisabledmanclient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.mvp.ViewPhotoActivity;
import com.clkj.cldisabledmanclient.util.ScreenUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/8/11.
 */

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {

    private List<String> photos;
    private Context context;

    public GalleryAdapter(List<String> photos, Context context) {
        this.photos = photos;
        this.context = context;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_photo, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        /*int targetHeight = ScreenUtils.getScreenHeight(context);
        int targetWidth = ScreenUtils.getScreenWidth(context);
        Picasso.get().load(photos.get(position)).resize(targetWidth/3,targetHeight/5).centerCrop().
                placeholder(R.mipmap.ico_data_empty).error(R.mipmap.ico_data_empty).into(viewHolder.ivPhoto);*/
        RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.mipmap.ico_data_empty).error(R.mipmap.ico_data_empty);
        Glide.with(context)
                .load(photos.get(position))
                .apply(requestOptions)
                .into(viewHolder.ivPhoto);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return photos.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivPhoto;

        public ViewHolder(View view) {
            super(view);
            ivPhoto = (ImageView) view.findViewById(R.id.iv_photo);

            ivPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<String> imgs = new ArrayList<String>();
                    for (String path : photos) {
                        imgs.add(path);
                    }
                    context.startActivity(ViewPhotoActivity.newIntent(context, imgs, getLayoutPosition(), true));
                }
            });
        }
    }
}
